package Eyjafjalla.action;

import Eyjafjalla.helper.EyjafjallaHelper;
import Eyjafjalla.helper.StringHelper;
import Eyjafjalla.modcore.EyjafjallaTag;
import Eyjafjalla.patch.CloudPatch;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.common.DrawCardAction;
import com.megacrit.cardcrawl.actions.utility.NewQueueCardAction;
import com.megacrit.cardcrawl.actions.utility.UnlimboAction;
import com.megacrit.cardcrawl.actions.utility.WaitAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.CardGroup;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.UIStrings;

import java.util.ArrayList;
import java.util.Iterator;

public class SayToSunAction extends AbstractGameAction {
    public SayToSunAction() {
        this.actionType = ActionType.CARD_MANIPULATION;
        this.startDuration = duration = Settings.ACTION_DUR_FAST;
        this.p = AbstractDungeon.player;
    }

    AbstractPlayer p;
    ArrayList<AbstractCard> fireCards;
    private static final UIStrings uiStrings;

    @Override
    public void update() {
        if (startDuration == duration) {
            fireCards = new ArrayList<>();
            for(AbstractCard c:p.exhaustPile.group){
                if(CloudPatch.CloudField.isFire.get(c)){
                    fireCards.add(c);
                }
            }
            if(fireCards.size()==0){
                this.isDone = true;
                return;
            }
            else {
                for(AbstractCard c:fireCards){
                    c.stopGlowing();
                    c.unhover();
                    c.unfadeOut();
                }
                CardGroup fire = new CardGroup(CardGroup.CardGroupType.UNSPECIFIED);
                fire.group.addAll(fireCards);
                AbstractDungeon.gridSelectScreen.open(fire, 1, uiStrings.TEXT[6], false);
            }
        }
        else {
            if (!AbstractDungeon.gridSelectScreen.selectedCards.isEmpty()) {
                Iterator<AbstractCard> c;
                AbstractCard card;
                for(c = AbstractDungeon.gridSelectScreen.selectedCards.iterator(); c.hasNext(); card.unhover()) {
                    card = c.next();
                    card.purgeOnUse = true;
                    AbstractDungeon.player.limbo.group.add(card);
                    card.current_y = (float) Settings.HEIGHT / 2.0F - 300.0F * Settings.scale;
                    card.target_x = (float) Settings.WIDTH / 2.0F + 200.0F * Settings.xScale;
                    card.target_y = (float) Settings.HEIGHT / 2.0F;
                    card.targetAngle = 0.0F;
                    card.lighten(false);
                    card.drawScale = 0.12F;
                    card.targetDrawScale = 0.75F;
                    card.applyPowers();
                    this.addToTop(new NewQueueCardAction(card, true, false, true));
                    this.addToTop(new UnlimboAction(card));
                    if (!Settings.FAST_MODE) {
                        this.addToTop(new WaitAction(Settings.ACTION_DUR_MED));
                    } else {
                        this.addToTop(new WaitAction(Settings.ACTION_DUR_FASTER));
                    }

                    this.p.exhaustPile.removeCard(card);
                }

                AbstractDungeon.gridSelectScreen.selectedCards.clear();

                for(c = this.p.exhaustPile.group.iterator(); c.hasNext(); card.target_y = 0.0F) {
                    card = c.next();
                    card.unhover();
                    card.target_x = (float)CardGroup.DISCARD_PILE_X;
                }
            }
        }

        this.tickDuration();
    }

    static {
        uiStrings = CardCrawlGame.languagePack.getUIString(StringHelper.CLOUD_OPERATION);
    }

}
